/*
 * Copyright (c) 2010-2017 Red Hat, Inc.
 */

#ifndef PIPE_H
#define PIPE_H

#include <string>

#include <windows.h>
#include <winternl.h>

#define CONNECTED L"\\CONNECTED"
#define CLOSE L"\\CLOSE"
#define CLOSED L"\\CLOSED"

#define PIPE_NAME L"\\\\.\\pipe\\vmgtrespipe"
#define APP_NAME L"vmgtresmgr.exe "
#define VMGTSHARE_PIPE_NAME L"\\\\.\\pipe\\vmgtsharepipe"
#define VMGTSHARE_APP_NAME L"vmgtsharemgr.exe "
#define DATA_BUFFER_LENGTH 1024
#define DATA_BUFFER_SIZE (DATA_BUFFER_LENGTH * sizeof(wchar_t))

class PipeClient {
public:
    PipeClient(std::wstring& sName);
    ~PipeClient(void);
    bool Init();
    void WaitRunning();
    void Close();
    HANDLE GetHandle();

private:
    void SetData(std::wstring& sData);
    void GetData(std::wstring& sData);
    bool ConnectToServer();
    static DWORD WINAPI ClientThread(LPVOID ptr);
    bool Read();
    bool Write();
    void Run();

private:
    const std::wstring m_sPipeName;  // Pipe name
    HANDLE m_hPipe;                  // Pipe handle
    HANDLE m_hThread;                // Pipe thread
    wchar_t* m_buffer;               // Buffer to hold data
};

class PipeServer {
public:
    PipeServer(const std::wstring& sName);
    ~PipeServer(void);
    bool Init();
    void Close();

private:
    static DWORD WINAPI ServerThread(LPVOID ptr);
    void Run();
    bool WaitForClient();
    void GetData(std::wstring& sData);
    void SetData(std::wstring& sData);
    bool Read();
    bool Write();

private:
    const std::wstring m_sPipeName;
    HANDLE m_hPipe;
    HANDLE m_hThread;
    wchar_t* m_buffer;
};

#endif // PIPE_H
